package com.shaokel.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shaokel.eduservice.entity.EduChapter;
import com.shaokel.eduservice.entity.EduVideo;
import com.shaokel.eduservice.entity.chapter.ChapterVo;
import com.shaokel.eduservice.entity.chapter.VideoVo;
import com.shaokel.eduservice.mapper.EduChapterMapper;
import com.shaokel.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaokel.eduservice.service.EduVideoService;
import com.shaokel.exception.shaokelException;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程 服务实现类
 *
 * @author 少丶客
 * @since 2020-06-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    //删除章节
    @Override
    public boolean deleteChapter(String courseId) {
        //先判断该章节有没有小节，如果存在小节，则必须先删除小节，才能删除章节
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        int count = eduVideoService.count(wrapper);
        if(count>0){//存在小节
            throw new shaokelException(20001,"不能进行删除");
        }else{//不存在小节，直接删除章节
            int result = baseMapper.deleteById(courseId);
            return result>0;
        }
    }

    //根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }

    //课程大纲列表，根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterVideoBycourseId(String courseId) {

        //1.根据courseId获取章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper< >();
        wrapperChapter.eq("course_id",courseId);
        wrapperChapter.orderByAsc("sort", "id");
        List<EduChapter> eduChaptersList = baseMapper.selectList(wrapperChapter);

        //2.根据courseId获取小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        wrapperChapter.orderByAsc("sort", "id");
        List<EduVideo> eduVideoList = eduVideoService.list(wrapperVideo);

        //最终封装的数据
        List<ChapterVo> finalList = new ArrayList<>();

        //3.封装章节数据
        for (int i = 0; i < eduChaptersList.size(); i++) {
            //每个章节
            EduChapter eduChapter = eduChaptersList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            //把eduChapter的数据封转到chapterVo
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //把chapterVo放到finaList
            finalList.add(chapterVo);

            //封装小节的数据
            List<VideoVo> videoList = new ArrayList<>();

            //4.封装小节数据
            for (int m = 0; m < eduVideoList.size(); m++) {
                EduVideo eduVideo = eduVideoList.get(m);
                //判断小节里面的chapterid和章节的id是否一致
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoList);
        }
        return finalList;
    }
}
